package com.eonmind.common.contract.exception.handler;


import com.eonmind.common.contract.data.response.ResponseEntity;
import com.eonmind.common.contract.enums.ErrorCode;
import com.eonmind.common.contract.exception.BusinessException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 全局异常处理器
 *
 * @author Xujie
 */
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    // 处理自定义异常
    @ExceptionHandler(BusinessException.class)
    public ResponseEntity<String> businessException(Exception e) {
        log.error("Exception:{}", e.getMessage(), e);
        return ResponseEntity.fail(ErrorCode.SYSTEM_ERROR, e.getMessage());
    }

    @ExceptionHandler(RuntimeException.class)
    public ResponseEntity<String> runtimeException(Exception e) {
        log.error("Exception:{}", e.getMessage(), e);
        return ResponseEntity.fail(ErrorCode.SYSTEM_ERROR, e.getMessage());
    }


}
